package com.ruoyi.merchant.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.merchant.domain.*;
import com.ruoyi.merchant.entity.H002Model;
import com.ruoyi.merchant.service.HealthCheckService;
import com.ruoyi.merchant.util.H002ModelCmdEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName HealthCheckService
 * @Description TODO
 * @Author chenxin.ju
 * @Date 2020-07-15 17:13
 * @ProjectName juservicecloud
 * @Version 1.0
 **/
@Slf4j
@Service
public class HealthCheckServiceImpl implements HealthCheckService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisService redisService;

    /**
     * 获取计步数据 24小时、7 周、30 月、120 季、360 年
     *
     * @param customerId
     * @return
     */
    @Override
    public List<C012DataBuShu> getStepData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<C012DataBuShu> c012DataBuShuList = mongoTemplate.find(condition, C012DataBuShu.class);
        log.info("c012DataBuShuList:{}",JSON.toJSONString(c012DataBuShuList));
        return c012DataBuShuList;
    }

    /**
     * 获取睡眠数据 24小时、7 周、30 月、120 季、360 年
     *
     * @param imei
     * @param day
     * @return
     */
    @Override
    public List<C012DataSleep> getSleepData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<C012DataSleep> c012DataBatList = mongoTemplate.find(condition, C012DataSleep.class);
        log.info("c012DataBatList:{}",JSON.toJSONString(c012DataBatList));
        return c012DataBatList;
    }

    /**
     * 获取心率数据 24小时、7 周、30 月、120 季、360 年
     *
     * @param imei
     * @param day
     * @return
     */
    @Override
    public List<C012DataBat> getBatData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<C012DataBat> c012DataBatList = mongoTemplate.find(condition, C012DataBat.class);
        log.info("c012DataBatList:{}",JSON.toJSONString(c012DataBatList));
        return c012DataBatList;
    }

    /**
     * 实时健康检测
     *
     * @param imei
     * @return
     */
    @Override
    public Boolean checkHealth(String imei) {
        return null;
    }


    /**
     * 获取实时健康数据
     * @param customerId
     */
    @Override
    public H002Model InstantHealthDetection(String customerId) {
        String strH002 = redisService.getObject(H002ModelCmdEnum.H002Model + customerId);
        H002Model h002Model = null;
        if(StringUtils.isNotBlank(strH002)) {
            h002Model = JSON.parseObject(strH002, new TypeReference<H002Model>() {});
        }
        log.info("h002Model:{}",JSON.toJSONString(h002Model));
        return h002Model;
    }

    @Override
    public List<C010DataBP> getBpData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<C010DataBP> c010DataBPList = mongoTemplate.find(condition, C010DataBP.class);
        log.info("C010DataBP:{}",JSON.toJSONString(c010DataBPList));
        return c010DataBPList;
    }

    @Override
    public List<TemperatureData> getTemperature(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<TemperatureData> temperatureDataList = mongoTemplate.find(condition, TemperatureData.class);
        log.info("TemperatureData:{}",JSON.toJSONString(temperatureDataList));
        return temperatureDataList;
    }

    @Override
    public List<RespiratoryRateData> getRespiratoryRate(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<RespiratoryRateData> respiratoryRateDataList = mongoTemplate.find(condition, RespiratoryRateData.class);
        log.info("RespiratoryRateData:{}",JSON.toJSONString(respiratoryRateDataList));
        return respiratoryRateDataList;
    }

    @Override
    public List<BloodSugarData> getBloodSugarData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<BloodSugarData> bloodSugarDataList = mongoTemplate.find(condition, BloodSugarData.class);
        log.info("BloodSugarData:{}",JSON.toJSONString(bloodSugarDataList));
        return bloodSugarDataList;
    }

    @Override
    public List<BloodOxygenData> getBloodOxygenData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<BloodOxygenData> bloodOxygenDataList = mongoTemplate.find(condition, BloodOxygenData.class);
        log.info("BloodSugarData:{}",JSON.toJSONString(bloodOxygenDataList));
        return bloodOxygenDataList;
    }

    @Override
    public List<CholesterolData> getCholesterolData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<CholesterolData> cholesterolDataList = mongoTemplate.find(condition, CholesterolData.class);
        log.info("CholesterolData:{}",JSON.toJSONString(cholesterolDataList));
        return cholesterolDataList;
    }

    @Override
    public List<UricAcidData> getUricAcidData(String customerId, int day) {
        Query condition= new Query();
        Date prec = DateUtils.getPreDay(day,new Date());;
        condition.addCriteria(Criteria.where("terminalTime").gte(prec));
        condition.addCriteria(Criteria.where("customerId").is(customerId));
        List<UricAcidData> uricAcidDataList = mongoTemplate.find(condition, UricAcidData.class);
        log.info("UricAcidData:{}",JSON.toJSONString(uricAcidDataList));
        return uricAcidDataList;
    }


}
